package com.wq.rentHouse.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wq.common.utils.Constant;
import com.wq.rentHouse.entity.UserEntity;
import com.wq.rentHouse.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wq.common.utils.PageUtils;
import com.wq.common.utils.Query;

import com.wq.rentHouse.dao.HouseDao;
import com.wq.rentHouse.entity.HouseEntity;
import com.wq.rentHouse.service.HouseService;

import javax.swing.*;


@Service("houseService" )
public class HouseServiceImpl extends ServiceImpl<HouseDao, HouseEntity> implements HouseService {

    @Autowired
    UserService userService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        final String key = (String) params.get("key" );
        final LambdaQueryWrapper<HouseEntity> lqw = new LambdaQueryWrapper<>();
        if (key != "" ) {
            lqw.eq(HouseEntity::getId, key).or().like(HouseEntity::getName, key);

            //售卖状态
            List<Integer> list;
            if (key.equals("售" )) {
                list = Arrays.asList(1, 2, 3);
                lqw.or().in(HouseEntity::getStatus, list);
            } else if ("在售".contains(key)) {
                lqw.or().eq(HouseEntity::getStatus, 1);
            } else if ("不出售".contains(key)) {
                lqw.or().eq(HouseEntity::getStatus, 0);
            } else if ("已售".contains(key)) {
                lqw.or().eq(HouseEntity::getStatus, 2);
            }

            //户主和租户
            final LambdaQueryWrapper<UserEntity> lqwUser = new LambdaQueryWrapper<>();
            lqwUser.like(UserEntity::getName, key);
            final UserEntity user = userService.getOne(lqwUser);
            if (user != null) {
                lqw.or().eq(HouseEntity::getTenantId, user.getId())
                        .or().eq(HouseEntity::getRenterId, user.getId());
            }
        }

        IPage<HouseEntity> page = this.page(new Query<HouseEntity>().getPage(params), lqw);
        return new PageUtils(page);
    }

}
